package com.hichao.cs.chat.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.TException;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.hichao.cs.helper.CSHelper;
import com.hichao.cs.thrift.chat.ChatClient;
import com.hichao.cs.thrift.front.FrontClient;

@Service
public class ChatService implements CSHelper {
	
	private String name = "WebFront";
	private String server_type = "repeater";
	private String ip = "192.168.1.61";
	private String port = "9090";
	private String token = "001";
	private int redisPort = 6379;
	private String redisHost = "192.168.1.62";
	
	public String push(String target, String text) {
		String rv = "Successful";
		ChatClient client = ChatClient.getClient("192.168.1.66", 9001);
		client.open();
		try {
			String source = "001";
			client.SendMessage(source, target, text);
		} catch (TException e) {
			e.printStackTrace();
		} finally {
			client.close();
		}
		return rv;
	}
	
	public String pushself(String text) {
		String rv = "Successful";
		FrontClient client = FrontClient.getClient("localhost", 9090);
		client.open();
		try {
			String source = "wfl";
			String target = "001";
			client.SendMessage(source, target, text);
		} catch (TException e) {
			e.printStackTrace();
		} finally {
			client.close();
		}
		return rv;
	}
	
	public Map<Long, String> pull(String source) {
		
		logger.info("server address: " + address.GetIntranetIpAddress());
		
		Map<Long, String> rv = new HashMap<Long, String>();
		Jedis jedis = new Jedis(this.redisHost, this.redisPort);
		String target = "001";
		String key = "com.hichao.cs.chat.pull." + "from." + source + ".to." + target;
		logger.info("key: {}", key);
		Map<String, String> value = jedis.hgetAll(key);
		if (value.isEmpty()) {
			jedis.close();
			return rv;
		} else {
			String[] tmp = new String[value.size()];
			String[] subKeyArray = value.keySet().toArray(tmp);
			for(String subKey:subKeyArray) {
				rv.put(Long.parseLong(subKey), value.get(subKey));
			}
			jedis.hdel(key, subKeyArray);
		}
		jedis.close();
		logger.info("get {}", rv);
		return rv;
	}
	
	public String signin() {
		String rv = "Successful";
		ChatClient client = ChatClient.getClient("192.168.1.66", 9001);
		client.open();
		try {
			boolean result = client.LoginServer(this.name, this.server_type, this.ip, this.port);
			if (result) {
				result = client.Login(this.name, this.token);
				logger.info("login successful!");
			}else
				logger.info("login failed!");
		} catch (TException e) {
			e.printStackTrace();
		}finally{
			client.close();
		}
		return rv;
	}
	
	public String signout() {
		String rv = "Successful";
		ChatClient client = ChatClient.getClient("192.168.1.66", 9001);
		client.open();
		try {
			client.Logout(this.name, this.token);
		} catch (TException e) {
			e.printStackTrace();
		}finally {
			client.close();
		}
		return rv;
	}	
}
